﻿
namespace Patterns4Net.PatternEnforcer.Core.Patterns
{
    using Patterns4Net.Core.Patterns;
    using Patterns4Net.Core.CecilExtensions;

    [PatternCheckerExport(typeof(Proxy))]
    public class ProxyChecker : WrapperChecker<Proxy>
    {
        public ProxyChecker()
        {
            this.If(pattern => pattern.Subject != null && pattern.RealSubject != null)
                .If(pattern => pattern.RealSubject.Resolve() != null)
                .Check(pattern => pattern.RealSubject.Resolve().Implements(pattern.Subject),
                       RealSubjectDoesNotImplementSubjectError);
        }

        private static string RealSubjectDoesNotImplementSubjectError(Proxy pattern)
        {
            return string.Format(
                ErrorMessages.ProxyRealSubjectDoesNotImplementSubject,
                pattern.RealSubject.Name,
                pattern.Subject.Name);
        }
    }
}
